package JMS;

import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.Stateless;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

import finals.Finals;

@Stateless
public class JMSServiceBean implements JMSService{

	private Logger logger = Logger.getLogger(Finals.LOGPATH);
	public void enviarJMS(String remitoXML, String cola, String ipDestino){
		Hashtable<String, String> props = new Hashtable<String, String>();


		props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		props.put(InitialContext.PROVIDER_URL, "jnp://"+ipDestino);


		try {
			InitialContext ctx = new InitialContext(props);
			QueueConnectionFactory qfactory = (QueueConnectionFactory) ctx
					.lookup("ConnectionFactory");
			Queue queue = (Queue) ctx.lookup("queue/"+cola);
			QueueConnection qCon = qfactory.createQueueConnection();
			QueueSession qSession = qCon.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			QueueSender qSender = qSession.createSender(queue);
			TextMessage message = qSession.createTextMessage();
			message.setText(remitoXML);
			qSender.send(message);

			logger.log(Level.INFO, "Se envio a "+cola+" el suiguiente remito:\n" + remitoXML);

		} catch (Exception e) {
			//manejar error!
			e.printStackTrace();
		}
	}
}
